package com.fm.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.fm.MDA;

/**
 * 安全适配器，用于判断登录状态、权限控制等
 * @author yangwc
 *
 */
@Component
public class SecurityInterceptor extends HandlerInterceptorAdapter {
	
	private static final Logger logger = LogManager.getLogger(SecurityInterceptor.class);


	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub
		super.afterCompletion(request, response, handler, ex);
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub
		super.postHandle(request, response, handler, modelAndView);
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		if("N".equals(MDA.SWITCH_CHECK_SESSION)){
			logger.debug("==SecurityInterceptor switch check session is close .");
			return true;		
		}
		// 处理之前进行判断
		logger.debug("==SecurityInterceptor check session.");
		//1.0是否登录
		Object seesionObj = request.getSession().getAttribute(MDA.SEESION_KEY);
		if(seesionObj == null){
			logger.warn("==SecurityInterceptor check session. session is null. sendRedirect to login!");
			//跳转到登录界面			
			return false;
		}else{
			//2.0是否有权限
			
		}
		
		return super.preHandle(request, response, handler);
	}
	

}
